﻿using System.IO;
using Ascend.Runtime.Core;
using Zove.DesignTime;
using Zove.Runtime;

namespace CommonModellingRuntime.Ascend
{
    public class AscendClassLoader : IClassLoader
    {
        #region IClassLoader Members

        public IClassFinder ClassFinder { get; set; }

        /// <summary>
        /// Load from source
        /// </summary>
        /// <param name="source">source</param>
        /// <returns>ZoomClass</returns>
        public ZoomClass LoadFromSource(string source)
        {
            var provider = new StringSourceProvider(source);

            var parser = new AscendClassParser(provider);

            return parser.Parse();
        }

        /// <summary>
        /// Load by class name
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public ZoomClass LoadByClassName(string className)
        {
            string location = ClassFinder.GetLocation(className);
            string source = File.ReadAllText(location);

            return LoadFromSource(source);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public ZoomClass LoadFromFile(string uri)
        {
            var provider = new FileSourceProvider(uri);

            var parser = new AscendClassParser(provider);

            return parser.Parse();
        }

        #endregion
    }
}